package com.cpi.basilos.user.service;

import KC.ModSegApp.UsuarioApp;

import com.cpi.basilos.user.converter.UserConverter;
import com.cpi.basilos.user.dao.UserDAO;
import com.cpi.basilos.user.dto.UserDTO;
import com.cpi.basilos.user.exception.UserException;
import com.cpi.user.view.UserView;
import com.cs.common.exception.CSException;

public class UserService {
	private static final String ERROR_INVALID_LOGIN_FOR_WAREHOUSE = "error.authentication.userforwarehouseinvalid";//$NON-NLS-1$

	public void loginUser(UserView userView) throws UserException {
		UserDAO userDAO = null;
		UserConverter userConverter = null;
		UserDTO userDTO = null;
		UsuarioApp usuario = null;
		try {
			userDAO = new UserDAO();
			userConverter = new UserConverter();
			userDTO = userConverter.convertUserViewToDto(userView);
			usuario = userDAO.loginUser(userDTO);
			if( !userDAO.validUserToApp(userDTO) ){
				UserException userException = null;
				userException = new UserException(ERROR_INVALID_LOGIN_FOR_WAREHOUSE,CSException.LAYER_SERVICE, CSException.ACTION_LOGIN);
				throw userException;
			}else{
				userView.setUserObject(usuario);
				userView.setWarehouseId(userDTO.getWarehouseId());
			}
		} catch (UserException userException) {
			throw userException;
		} catch (Exception e){
			UserException userException = null;
			e.printStackTrace(System.out);
			userException = new UserException(e , CSException.LAYER_SERVICE,CSException.ACTION_LISTS);
			throw userException;
		}
	}
	
}
